package ren.kane.phoenix.thread;

/**
 * @author renrk(1275) on 2019-08-15 2:40 PM
 **/
public class InterruptTest {
    public static void main(String[] args) {
        try {
            Thread t = new ThreadE("t");
            System.out.println(t.getName() + " (" + t.getState() + ") is new.");
            t.start();
            System.out.println(t.getName() + " (" + t.getState() + ") is started.");
            Thread.sleep(300);
            t.interrupt();
            System.out.println(t.getName() + " (" + t.getState() + ") is interrupted.");
            Thread.sleep(300);
            System.out.println(t.getName() + " (" + t.getState() + ") is interrupted now.");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

class ThreadE extends Thread {
    public ThreadE(String name) {
        super(name);
    }

    @Override
    public void run() {
        try {
            int i = 0;
            while (!isInterrupted()) {
                Thread.sleep(100);
                i++;
                System.out.println(Thread.currentThread().getName() + " (" + this.getState() + ") loop " + i);
            }
        } catch (InterruptedException e) {
            System.out.println(Thread.currentThread().getName() + " (" + this.getState() + ") catch InterruptedException.");
        }
    }
}